package com.jjvu.news.controller;

import com.github.pagehelper.PageInfo;
import com.jjvu.news.dao.Sys_user;
import com.jjvu.news.service.LeftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LeftController {
    @Autowired
    private LeftService leftService;
    @RequestMapping("/findUserPageAll")
    public String findArticlePageAll(@RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                                     @RequestParam(value = "size",required = true,defaultValue = "5")Integer size,Model model) {
        List<Sys_user> listUser=leftService.findUserPageall(page, size);
        PageInfo<Sys_user> pageInfo=new PageInfo<Sys_user>(listUser);
        Integer pageIndex=1;
        model.addAttribute("listUser",listUser);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageIndex);
        return "admin/UserManager";
    }
    @RequestMapping("/findUserById")
    public String findUserById(Integer uid,Model model){
        Sys_user UId=leftService.findUserById(uid);
        model.addAttribute("UId",UId);
        return "admin/UserEdit";
    }
    @RequestMapping("/updateUser")
    public String updateUser(Sys_user user){
        if(leftService.updateUser(user)) {
            return "redirect:/findUserPageAll";
        }
        return "forward:/findUserById?uid="+user.getUid();
    }
    @RequestMapping("/addUser")
    public String addUser(Sys_user user, Model model){
        String Msg;
        if (leftService.findUserBy(user)){
            Msg="用户名已被使用";
            model.addAttribute("Msg",Msg);
            return "admin/AddUser";
        }
        if (leftService.addUser(user)){
            return "redirect:/findUserPageAll";
        }
        return null;

    }
    @RequestMapping("/deleteUser")
    public String deleteUser(Integer uid,HttpSession session){
        if(leftService.deleteUser(uid)){
            return "redirect:/findUserPageAll";
        }
        return "redirect:/findUserPageAll";
    }
    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "admin/AddUser";
    }
}
